using System.Threading;
using WatiN.Core;

namespace Sketch.Test.Acceptance
{
    public static class WebBrowserExtensions
    {
        public static void WaitForAjaxRequest(this Browser browser)
        {
            int timeWaitedInMilliseconds = 0;
            var maxWaitTimeInMilliseconds = Settings.WaitForCompleteTimeOut * 1000;
            while (browser.IsAjaxRequestInProgress() && timeWaitedInMilliseconds < maxWaitTimeInMilliseconds)
            {
                Thread.Sleep(Settings.SleepTime);
                timeWaitedInMilliseconds += Settings.SleepTime;
            }
        }
        public static bool IsAjaxRequestInProgress(this Browser browser)
        {
            var evalResult = browser.Eval("window.watinAjaxMonitor.isRequestInProgress()");
            return evalResult == "true";
        }        
    }
}